﻿using Microsoft.MixedReality.Toolkit.Utilities;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Shows_3D
{
    public class TwoPointsLineDataProvider : BaseMixedRealityLineDataProvider
    {
        [SerializeField]
        private Transform _startPoint;
        [SerializeField]
        private Transform _endPoint;
        private MixedRealityPose startPoint = new MixedRealityPose();
        public MixedRealityPose StartPoint;
        private MixedRealityPose endPoint = new MixedRealityPose();
        public MixedRealityPose EndPoint;
        
        private void Start()
        {
            startPoint.Position = _startPoint.position;
            endPoint.Position = _endPoint.position;
            StartPoint = startPoint;
            EndPoint = endPoint;
        }

        private void Update()
        {
            startPoint.Position = _startPoint.position;
            endPoint.Position = _endPoint.position;
            StartPoint = startPoint;
            EndPoint = endPoint;
        }
        #region Line Data Provider Implementation

        /// <inheritdoc />
        public override int PointCount => 2;

        /// <inheritdoc />
        protected override Vector3 GetPointInternal(int pointIndex)
        {
            switch (pointIndex)
            {
                case 0:
                    return StartPoint.Position;
                case 1:
                    return EndPoint.Position;
                default:
                    Debug.LogError("Invalid point index");
                    return Vector3.zero;
            }
        }

        /// <inheritdoc />
        protected override void SetPointInternal(int pointIndex, Vector3 point)
        {
            switch (pointIndex)
            {
                case 0:
                    startPoint.Position = point;
                    break;
                case 1:
                    endPoint.Position = point;
                    break;
                default:
                    Debug.LogError("Invalid point index");
                    break;
            }
        }

        /// <inheritdoc />
        protected override Vector3 GetPointInternal(float normalizedDistance)
        {
            return Vector3.Lerp(StartPoint.Position, EndPoint.Position, normalizedDistance);
        }

        /// <inheritdoc />
        protected override float GetUnClampedWorldLengthInternal()
        {
            return Vector3.Distance(StartPoint.Position, EndPoint.Position);
        }

        /// <inheritdoc />
        protected override Vector3 GetUpVectorInternal(float normalizedLength)
        {
            return transform.up;
        }

        #endregion Line Data Provider Implementation
    }
}